package cn.pconline.autoclub.service.bbssync;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import cn.pconline.autoclub.entity.Club;
import cn.pconline.autoclub.entity.ClubIntegral;
import cn.pconline.autoclub.entity.ClubMember;
import cn.pconline.autoclub.entity.User;
import cn.pconline.autoclub.service.DynaService;
import cn.pconline.autoclub.service.PetrolService;

import com.alibaba.fastjson.JSONObject;

/**
 * @author zhouruxuan
 * @date 2014年12月4日 上午11:13:53
 */
public class SyncDeleteReplyService extends BbsSyncPetrolService {
	@Autowired
	private DynaService dynaService;
	@Autowired
	private PetrolService petrolService;
	
    @Override
    protected int addPetroltoMember(ClubMember clubMember, JSONObject msg) {
        int agent = getAgent(msg);
        ClubIntegral clubIntegral;
        if (agent <= 0) {
            clubIntegral = petrolService.getById(PetrolService.ADD_PERSONAL_PETROL_ITEM_POST_DELETE);
        } else {
            clubIntegral = petrolService.getById(PetrolService.ADD_PERSONAL_PETROL_ITEM_POST_WAP_DELETE);
        }
        if(clubIntegral != null) {
            String reason = clubIntegral.getName() + "(topicId-" + getTopicId(msg) + ", postId-" + getPostId(msg) + ")";
            petrolService.addOnlyPersonWithinLimit(clubMember, (int)clubIntegral.getIntegralId(), clubIntegral.getPetrol(), reason, true);
            return clubIntegral.getPetrol();
        }
        return 0;
    }

    @Override
    protected int addPetroltoClub(Club club, JSONObject msg) {
        int agent = getAgent(msg);
        ClubIntegral clubIntegral;
        if (agent <= 0) {
            clubIntegral = petrolService.getById(PetrolService.ADD_PERSONAL_PETROL_ITEM_POST_DELETE);
        } else {
            clubIntegral = petrolService.getById(PetrolService.ADD_PERSONAL_PETROL_ITEM_POST_WAP_DELETE);
        }
        if(clubIntegral != null) {
            String reason = clubIntegral.getName() + "(topicId-" + getTopicId(msg) + ", postId-" + getPostId(msg) + ")";
            petrolService.addToClubWithinLimit(club.getClubId(), (int)clubIntegral.getIntegralId(), clubIntegral.getPetrol(), reason, false);
            return clubIntegral.getPetrol();
        }
        return 0;
    }

    @Override
    protected long getUserId(JSONObject msg) {
        return msg.getLongValue("postAuthorId");
    }
	
	@Override
	public void process(JSONObject msg, ClubMember clubMember) {
	    
		//验证用户
	    long userId = getUserId(msg);
	    if (userId <= 0L) {
            log.info("not userId-{}", userId);
            return;
        }

        //判断用户是否登录
        User user = userService.authUser(userId);
        if (user == null || user.getUid() == 0L) {
            log.info("not found user-{}", userId);
            return;
        }

        //删除回帖
        Date createAt = msg.getDate("topicCreateAt");
        dynaService.deleteDynaReply(getTopicId(msg), createAt, getPostId(msg));
	}
	
	private int getAgent(JSONObject msg) {
	    return msg.getIntValue("agent");
	}
	
	private long getTopicId(JSONObject msg) {
	    return msg.getLongValue("topicId");
	}
	
	private long getPostId(JSONObject msg) {
        return msg.getLongValue("postId");
    }
}
